import 'package:casdon/modules/recipe/data/api/recipe_search_list_api.dart';
import 'package:casdon/modules/recipe/data/model/recipe_placeholder_data.dart';

class RecipeSearchModel {
  final String id;
  final String? mainImg;
  final String name;
  final bool isControl;
  final int? cookTime;
  final String? cookTimeUnit;
  final String easyLevelDesc;
  final String? view;
  final String? collection;
  final String? likes;
  final bool isvideo;

  RecipeSearchModel({
    required this.id,
    required this.mainImg,
    required this.name,
    required this.isControl,
    required this.cookTime,
    required this.cookTimeUnit,
    required this.easyLevelDesc,
    required this.collection,
    required this.view,
    required this.likes,
    required this.isvideo,
  });

  // 静态方法转换
  static RecipeSearchModel fromApi(RecipeSearchItem response) {
    final rawVideoCover = (response.videoCoverUrls ?? "").trim();
    final rawImg = (response.mainImg ?? response.imageUrls ?? "").trim();
    String resolvedImg;

    if (rawVideoCover.isNotEmpty) {
      resolvedImg = rawVideoCover.split(",").first;
    } else if (rawImg.isNotEmpty) {
      resolvedImg = rawImg.split(",").first;
    } else {
      resolvedImg = recipePlaceHolderImgUrl;
    }

    return RecipeSearchModel(
      id: response.id,
      name: response.name,
      mainImg: resolvedImg,
      cookTime: response.cookTime ?? 0,
      cookTimeUnit: response.cookTimeUnit ?? "分",
      collection: response.collection ?? "0",
      view: response.view ?? "0",
      likes: response.likes ?? "0",
      isControl: (response.isControl ?? 0) == 1 ? true : false,
      easyLevelDesc: easyLevelConvert(response.easyLevelDesc),
      isvideo: (response.videoCoverUrls ?? "").trim().isNotEmpty,
    );
  }

  static String easyLevelConvert(String? level) {
    if (level == "1") {
      return "入门";
    }

    if (level == "2") {
      return "简单";
    }

    if (level == "3") {
      return "中等";
    }

    if (level == "4") {
      return "进阶";
    }
    if (level == "5") {
      return "困难";
    }
    return "中等";
  }

  // 批量转换的静态方法
  static List<RecipeSearchModel> listFromApi(List<RecipeSearchItem> items) {
    return items.map(fromApi).toList();
  }
}
